package com.example.ch07_01_08_spinnerdemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	//private String[] colors;
	private Spinner spinner;
	private TextView showColor;
	private LinearLayout rootLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHandler();
	}

	private void initView() {
		// TODO Auto-generated method stub
		//colors = this.getResources().getStringArray(R.array.Colors);
		spinner = (Spinner) this.findViewById(R.id.spinner1);
		showColor = (TextView) this.findViewById(R.id.showColor);
		rootLayout = (LinearLayout) this.findViewById(R.id.rootLayout);
		// 準備一個陣列配接器
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.Colors, android.R.layout.simple_spinner_item);
		// 設置下拉樣式
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 為下拉清單設置配接器
		spinner.setAdapter(adapter);
	}

	private void initHandler() {
		// TODO Auto-generated method stub
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String s = arg0.getItemAtPosition(arg2).toString();
				Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
				if(s.equals("Red")){
					showColor.setTextColor(Color.RED);
					rootLayout.setBackgroundColor(Color.WHITE);
				}else if(s.equals("Green")){
					showColor.setTextColor(Color.GREEN);
					rootLayout.setBackgroundColor(Color.BLUE);
				}else if(s.equals("Blue")){
					showColor.setTextColor(Color.BLUE);
					rootLayout.setBackgroundColor(Color.GREEN);
				}else{
					showColor.setTextColor(Color.WHITE);
					rootLayout.setBackgroundColor(Color.RED);
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
